<?php
namespace app\admin\controller;

use app\common\controller\Back;

class Link extends Back
{
    private $pageSize = 20;
    
    protected function initialize()
    {
        parent::initialize();
    }
    
    public function index()
    {
        $args = [
            'where' => [],
            'order' => [
                'link_order' => 'asc',
            ],
        ];
        //查询数据
        $list = model('common/Link','loglic')->select($args);
        // 模板变量赋值
        $this->assign('list', $list);
        $this->assign($this->query);
        $this->assign($this->site);
        // 渲染模板输出
        return $this->fetch();
    }

    public function save()
    {
        $post = input('post.');
        //批量删除
        if($post['delete']){
            model('common/Link','loglic')->deleteId($post['delete']);
            foreach($post['delete'] as $key=>$id){
                unset($post['name'][$id]);
            }
        }
        //批量修改
        foreach($post['name'] as $id=>$name){
            $data = [];
            $data['link_id']     = $id;
            $data['link_name']   = $name;
            $data['link_url']    = $post['url'][$id];
            $data['link_info']   = $post['info'][$id];
            $data['link_logo']   = $post['logo'][$id];
            $data['link_order']  = intval($post['order'][$id]);
            $data['link_status'] = intval($post['status'][$id]);
            model('common/Link','loglic')->updatePk($data);
        }
        //批量新增
        $list = [];
        foreach($post['newname'] as $key=>$name){
            $list[$key]['link_name']   = $name;
            $list[$key]['link_url']    = $post['newurl'][$key];
            $list[$key]['link_info']   = $post['newinfo'][$key];
            $list[$key]['link_logo']   = $post['newlogo'][$key];
            $list[$key]['link_order']  = intval($post['neworder'][$key]);
            $list[$key]['link_status'] = intval($post['newstatus'][$key]);
        }
        if($list){
            model('common/Link','loglic')->saveAll($list);
        }
        //返回结果
        $this->success('操作完成');
    }
}